﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yto.Common.logging;

namespace Yto.Threading.BackgroundWorkers
{
    public abstract class TimedBgdWorker : BaseBgdWorker
    {
        protected readonly BaseTimer baseTimer;

        public TimedBgdWorker(BaseTimer timer)
        {
            baseTimer = timer;
            baseTimer.Elasped += Timer_Elasped;
        }

        private void Timer_Elasped(object sender, EventArgs e)
        {
            try
            {
                DoWork();
            }
            catch (Exception ex)
            {
                Logger.Warn(ex.Message, ex);
            }
        }

        protected abstract void DoWork();

        public override void Start()
        {
            base.Start();
            baseTimer.Start();
        }

        public override void Stop()
        {
            baseTimer.Stop();
            base.Stop();
        }

        public override void Wait2Stop()
        {
            baseTimer.Wait2Stop();
            base.Wait2Stop();
        }
    }
}
